package com.samoiloff.ui.list.simple {
	import com.samoiloff.ui.btns.BrightBtnController;

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * @author Samoiloff  http://samoiloff.com/
	 */
	public class ScrollContBasicArrows {
		
		private var _mc:MovieClip;
		private var _scrollCont : ScrollCont;
		
		private var _leftBtn:BrightBtnController;
		private var _rightBtn:BrightBtnController;
		
		private var _leftEndBtn:BrightBtnController;
		private var _rightEndBtn:BrightBtnController;
		
		public function ScrollContBasicArrows(mc:MovieClip, scrollCont:ScrollCont) {
			_mc = mc;
			_scrollCont = scrollCont;
			
			_leftBtn = new BrightBtnController(_mc.leftMc);
			_rightBtn = new BrightBtnController(_mc.rightMc);
			_leftEndBtn = new BrightBtnController(_mc.leftEndMc);
			_rightEndBtn = new BrightBtnController(_mc.rightEndMc);
			
			
			_leftBtn.addEventListener(MouseEvent.CLICK, onLeftClick);
			_rightBtn.addEventListener(MouseEvent.CLICK, onRightClick);
			_leftEndBtn.addEventListener(MouseEvent.CLICK, onLeftEndClick);
			_rightEndBtn.addEventListener(MouseEvent.CLICK, onRightEndClick);

			_scrollCont.addEventListener(ScrollCont.DATA_CHANGED, onDataChanged);
			
			updateEnabled();
		}

		private function onDataChanged(event : Event) : void {
			updateEnabled();
		}
		
		public function destroy():void {
			_scrollCont.removeEventListener(ScrollCont.DATA_CHANGED, onDataChanged);
			
			_leftBtn.removeEventListener(MouseEvent.CLICK, onLeftClick);
			_rightBtn.removeEventListener(MouseEvent.CLICK, onRightClick);
			_leftEndBtn.removeEventListener(MouseEvent.CLICK, onLeftEndClick);
			_rightEndBtn.removeEventListener(MouseEvent.CLICK, onRightEndClick);
			
			_mc = null;
			_scrollCont = null;
			
			_leftBtn = null;
			_rightBtn = null;
			
			_leftEndBtn = null;
			_rightEndBtn = null;
			
		}
		
		private function onLeftClick(e:MouseEvent):void {
			_scrollCont.anchorIndex--;
			updateEnabled();
		}

		private function onRightClick(e:MouseEvent):void {
			_scrollCont.anchorIndex++;
			updateEnabled();
		}
		
		private function onLeftEndClick(e:MouseEvent): void {
			if (_scrollCont.data) {
				// _scrollCont.anchorIndex = 0;
				_scrollCont.anchorIndex -= _scrollCont.widthInItems;
				updateEnabled();
			}
		}

		private function onRightEndClick(e:MouseEvent): void {
			// _scrollCont.anchorIndex = _scrollCont.data.length - 7;
			_scrollCont.anchorIndex += _scrollCont.widthInItems;
			updateEnabled();
		}
		
		public function updateEnabled():void {
			_leftBtn.mc.visible = _leftEndBtn.mc.visible = _scrollCont.anchorIndex > 0;
			_rightBtn.mc.visible = _rightEndBtn.mc.visible = (_scrollCont.anchorIndex + _scrollCont.widthInItems) < _scrollCont.data.length;
		}

		public function get mc() : MovieClip {
			return _mc;
		}
		
	}
}
